package com.cxps.server.inventory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxps.cloud.utils.IdUtils;
import com.cxps.cloud.utils.NumUtil;
import com.cxps.cloud.utils.ObjectUtil;
import com.cxps.server.inventory.domain.*;
import com.cxps.server.inventory.mapper.InventoryInfoMapper;
import com.cxps.server.inventory.service.InventoryInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;

/**
 * <p>
 * 商品库存信息表 服务实现类
 * </p>
 *
 * @author YouCai.Liu
 * @since 2021-11-02
 */
@Service
public class InventoryInfoServiceImpl implements InventoryInfoService {

    @Autowired
    private InventoryInfoMapper inventoryInfoMapper;


    /**
     * 商品库存信息列表
     *
     * @param query
     * @return
     */
    @Override
    public Page list(InventoryInfoQuery query) {

        // 创建分页对象
        Page<InventoryInfoVo> page = new Page<>(query.getCurrent(), query.getSize());

        // 设置排序字段

        // 执行查询
        List<InventoryInfoVo> voList = inventoryInfoMapper.list(page, query);
        page.setRecords(voList);

        return page;
    }

    /**
     * 库存预警设置
     *
     * @param warning
     */
    @Override
    public void warning(InventoryWarning warning) throws IllegalAccessException {

        Integer setType = warning.getSetType();

        String[] ids = warning.getIds();
        // setType等于0 并且 ids不为空
        if ((null != setType && setType == 0) && (null == ids || ids.length == 0)) {
            throw new IllegalArgumentException("选中设置情况下，请选择需要设置的商品");
        }

        InventoryInfoQuery query = warning.getQuery();
        // setType等于1 并且 query对象里的所有属性至少有一个有值
        if ((null != setType && setType == 1) && !ObjectUtil.checkObjFieldIsNull(query)) {
            throw new IllegalArgumentException("筛选设置情况下，请至少有一个查询条件");
        }

        // 设置上限
        if (null != setType && setType == 0) {
            inventoryInfoMapper.updateWarningByIds(ids, warning.getInventoryMin(), warning.getInventoryMax());
        } else if (null != setType && setType == 1) {
            inventoryInfoMapper.updateWarningByQuery(query, warning.getInventoryMin(), warning.getInventoryMax());
        }
    }

    /**
     * 商品库存流水列表
     *
     * @param query
     * @return
     */
    @Override
    public Page flowingList(InventoryInfoQuery query) {
        // 创建分页对象
        Page<InventoryFlowingVo> page = new Page<>(query.getCurrent(), query.getSize());

        // 设置排序字段

        // 执行查询
        List<InventoryFlowingVo> voList = inventoryInfoMapper.flowingList(page, query);
        page.setRecords(voList);

        return page;
    }


    /**
     * 商品入库
     *
     * @param productId
     * @param kcNum
     */
    @Override
    @Transactional
    public void add(String productId, Integer kcNum) {
        // 设置库存
        QueryWrapper qw = new QueryWrapper();
        qw.eq("product_id", productId);
        qw.select("id", "usable_inventory");
        InventoryInfoEntity inventoryInfoEntity = inventoryInfoMapper.selectOne(qw);
        if (null == inventoryInfoEntity) {
            InventoryInfoEntity entity = new InventoryInfoEntity();
            entity.setId(IdUtils.generateId());
            entity.setProductId(productId);
            entity.setUsableInventory(kcNum);
            entity.setLockInventory(10);
            entity.setInventoryMin(10);
            entity.setInventoryMax(20);
            entity.preInsert();
            inventoryInfoMapper.insert(entity);
        } else {
            Integer usableInventory = inventoryInfoEntity.getUsableInventory();
            inventoryInfoEntity.setUsableInventory(usableInventory + kcNum);
            inventoryInfoMapper.updateById(inventoryInfoEntity);
        }

        // 设置库存流水
        InventoryKcInfo inventoryKcInfo = new InventoryKcInfo();
        inventoryKcInfo.setId(IdUtils.generateId());
        inventoryKcInfo.setRkOdd("RK" + NumUtil.getNum());
        inventoryKcInfo.setRkNum(kcNum);
        inventoryKcInfo.setProductId(productId);
        inventoryInfoMapper.saveInventoryKcInfo(inventoryKcInfo);
    }

    /**
     * 商品库存流水
     *
     * @param productId
     */
    @Override
    public List<InventoryKcInfoVo> runningWater(String productId) {
        List<InventoryKcInfoVo> kcInfoVos = inventoryInfoMapper.selectRunningWater(productId);

        return kcInfoVos;
    }

}
